/**
 * Copyright (C) 2009 fgrilli <federico.grilli@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.google.code.mgnlgroovy.scheduler.admin;

import info.magnolia.cms.gui.control.ContextMenu;
import info.magnolia.cms.gui.control.ContextMenuItem;
import info.magnolia.cms.gui.control.FunctionBar;
import info.magnolia.cms.gui.controlx.Renderer;
import info.magnolia.cms.gui.controlx.list.IconListColumnRenderer;
import info.magnolia.cms.gui.controlx.list.ListColumn;
import info.magnolia.cms.gui.controlx.list.ListControl;
import info.magnolia.cms.gui.controlx.list.ListModel;
import info.magnolia.cms.gui.controlx.search.SearchConfig;
import info.magnolia.cms.i18n.Messages;
import info.magnolia.context.MgnlContext;
import info.magnolia.module.admininterface.lists.AbstractSimpleSearchList;
import info.magnolia.module.admininterface.lists.AdminListControlRenderer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author fgrilli
 * @version $Id: SchedulerJobList.java 3 2009-04-16 22:48:19Z federico.grilli $
 */
public class SchedulerJobList extends AbstractSimpleSearchList
{

    public SchedulerJobList(String name, HttpServletRequest request, HttpServletResponse response)
    {
        super(name, request, response);
    }

    public void configureList(ListControl list)
    {
        final Messages msgs = getMsgs();

        AdminListControlRenderer renderer = new AdminListControlRenderer()
        {

            {
                setJavaScriptClass("mgnl.scheduler.JobList");
            }

            protected String buildJavaScriptObject(ListControl list, Object value)
            {
                return super.buildJavaScriptObject(list, value) + ", path: '" + list.getIteratorValue("path") + "'";
            }

        };

        list.setRenderer(renderer);

        list.addColumn(new ListColumn()
        {

            {
                setName("active");
                setColumnName("active");
                setLabel(msgs.get("list.column.active"));
                setWidth("55px");
            }

            public Object getValue()
            {
                Boolean status = Boolean.valueOf(super.getValue().toString());
                StringBuilder str = new StringBuilder("/.resources/icons/16/flag_");

                if (status == null)
                {
                    str.append("yellow.gif");
                }
                else if (status.booleanValue())
                {
                    str.append("green.gif");
                }
                else
                {
                    str.append("red.gif");
                }

                return str.toString();
            }

            public Renderer getRenderer()
            {
                return new IconListColumnRenderer();
            }
        });

        list.addColumn(new ListColumn("title", msgs.get("list.column.jobName"), "150", true));
        list.addColumn(new ListColumn("lastFireTime", msgs.get("list.column.lastFireTime"), "150", true));
        list.addColumn(new ListColumn()
        {

            {
                setName("terminatedWithError");
                setColumnName("terminatedWithError");
                setLabel(msgs.get("list.column.status"));
                setWidth("55px");
            }

            public Object getValue()
            {
                Boolean status = Boolean.valueOf(super.getValue().toString());
                StringBuilder str = new StringBuilder("/.resources/scheduler/css/images/");

                if (status == null)
                {
                    str.append("warning.png");
                }
                else if (status.booleanValue())
                {
                    str.append("error.png");
                }
                else
                {
                    str.append("success.png");
                }

                return str.toString();
            }

            public Renderer getRenderer()
            {
                return new IconListColumnRenderer();
            }
        });

        list.addColumn(new ListColumn("nextFireTime", "Next fire time", "150", true));
        list.addColumn(new ListColumn("startTime", msgs.get("list.column.startTime"), "150", true));
        list.addColumn(new ListColumn("endTime", msgs.get("list.column.endTime"), "150", true));
        list.addColumn(new ListColumn("cron", msgs.get("list.column.cron"), "100", true));
        list.addColumn(new ListColumn("description", msgs.get("list.column.jobDescription"), "200", true));

        list.addSortableField("active");
        list.addSortableField("title");
        list.addSortableField("lastFireTime");
        list.addSortableField("terminatedWithError");
        list.addSortableField("nextFireTime");
        list.addSortableField("startTime");
        list.addSortableField("endTime");
    }

    protected void configureContextMenu(ContextMenu menu)
    {
        addContextMenuItem(menu, "newJob", "list.config.menu.newJob", "clock_add.png", "newJob", "showNewJobInCtxMenu");
        addContextMenuItem(menu, "editJob", "list.config.menu.editJob", "clock_edit.png", "editJob", "isSelected");
        menu.addMenuItem(null);
        addContextMenuItem(
            menu,
            "deleteJob",
            "list.config.menu.deleteJob",
            "clock_delete.png",
            "deleteJob",
            "isSelected");
    }

    public ListModel getModel()
    {
        return new SchedulerJobListModel("config");
    }

    protected void configureFunctionBar(FunctionBar bar)
    {
        super.configureFunctionBar(bar);

        bar.addMenuItem(getContextMenu().getMenuItemByName("newJob"));
        bar.addMenuItem(getContextMenu().getMenuItemByName("editJob"));
        bar.addMenuItem(getContextMenu().getMenuItemByName("deleteJob"));
        bar.setSearchable(false);
    }

    public SearchConfig getSearchConfig()
    {
        return null;
    }

    /**
     * Helper method to creat menu items for the list
     */
    protected void addContextMenuItem(ContextMenu menu, String name, String label, String iconName, String methodName,
        String isActiveMethodName)
    {
        final ContextMenuItem showInTree = new ContextMenuItem(name);
        showInTree.setLabel(getMsgs().get(label));
        showInTree.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/" + iconName);
        showInTree.setOnclick(this.getList().getName() + "." + methodName + "();");
        showInTree.addJavascriptCondition("function(){return "
            + this.getList().getName()
            + "."
            + isActiveMethodName
            + "()}");
        menu.addMenuItem(showInTree);
    }
}